package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired(required = false)
    private ShoppingCartService shoppingCartService;
    @RequestMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        Long id = (Long) session.getAttribute("user");
        shoppingCart.setUserId(id);
        ShoppingCart dbShoppingCart = shoppingCartService.add(shoppingCart);
        return R.success(dbShoppingCart);
    }

    @RequestMapping("/list")
    public R<List<ShoppingCart>> findAll(HttpSession session){
        Long id = (Long) session.getAttribute("user");
        List<ShoppingCart> shoppingCartList = shoppingCartService.findAll(id);
        return R.success(shoppingCartList);
    }

    @RequestMapping("/clean")
    public R<String> clean(HttpSession session){
        Long id = (Long) session.getAttribute("user");
        shoppingCartService.clean(id);
        return R.success("购物车已清空");
    }

    @RequestMapping("/sub")
    public R<String> updateNumber2(@RequestBody ShoppingCart shoppingCart,HttpSession session){
        Long id = (Long) session.getAttribute("user");
        shoppingCart.setUserId(id);
        shoppingCartService.updateNumber2(shoppingCart);
        return R.success("移除成功");
    }
}
